<?php
/**
 * RuiNuoCode低代码后台系统 - 团队十多年低代码经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 张家界瑞诺科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.ruinuocrm.com
 * =========================================================
 */
namespace app\admin\service\recycle;
use app\admin\service\recycle\Logic as RuinuoLogic;//数据导入表单服务
use app\common\service\Excel as ExcelService;//导入导出服务
use app\common\service\Lists as ListsService;//列表服务
use app\common\service\Power as PowerService;//权限服务
use app\common\model\AdminRecycle as DataMode;//数据模型

class Lists
{
    public function Mode()
    {
        $Model=new DataMode();
        $DB=$Model->alias('a');
        return $DB;
    }
    public function SetWhere($current=1)
    {
        $Power=new PowerService();
        $where='1=1 and a.is_delete=0';

        $where.=$Power->IndexWhere('admin:recycle');
        return $where;
    }

    public function QueryWhere($params = [])
    {
        $where=[];
        if (!empty($params['keyword'])) {
            $where[] = ['a.table_name|a.app|a.controller|a.action|a.delete_ip', 'like', '%' . $params['keyword'] . '%'];
        }
        if (!empty($params['delete_type'])) {
            $where[] = ['a.delete_type', '=', $params['delete_type']];
        }
        if (!empty($params['is_status'])) {
            $where[] = ['a.is_status', '=', $params['is_status']];
        }
        if (!empty($params['choose_ids'])){
            $where[] = ['a.id', 'in', $params['choose_ids']];
        }

        return $where;
    }

    public function Tab($params = [])
    {
        $data=[];
        return $data;
    }
    public function GetTab($params=[],$current=1)
    {
        return $this->Mode()->where($this->SetWhere($current))->where($this->QueryWhere($params))->count();
    }

    public function Sum($params = [])
    {
        $data=[];
        return $data;
    }

    public function GetSum($params=[],$field='id')
    {
        $current=!empty($params['current']) ? $params['current'] : 1;
        return $this->Mode()->where($this->SetWhere($current))->where($this->QueryWhere($params))->sum($field);
    }

    public function Total($params = [])
    {
        $current= !empty($params['current']) ? $params['current'] : 1;
        $count = $this->Mode()
            ->where($this->SetWhere($current))
            ->where($this->QueryWhere($params))
            ->count();
        return $count;
    }

    public function Lists($params = [])
    {
        $Power=new PowerService();
        $page = !empty($params['page']) ? $params['page'] - 1 : 0;
        $limit = !empty($params['limit']) ? $params['limit'] : 1000000;
        $current= !empty($params['current']) ? $params['current'] : 1;
        $orderRaw=!empty($params['raw']) ? $params['raw'] : 'a.create_time desc';
        $field=$Power->IndexField('a.id,a.table_name,a.app,a.controller,a.action,a.delete_ip,a.delete_id,a.delete_type,a.is_status,a.create_id,a.create_time','admin:recycle');
        $rows= $this->Mode()
            ->field($field)
            ->where($this->SetWhere($current))
            ->where($this->QueryWhere($params))
            ->orderRaw($orderRaw)
            ->limit($page * $limit, $limit)
            ->select()
            ->toArray();
        $ListsService = new ListsService();
        $rows=$ListsService->ListData($rows);//转换数据
        return $rows;
    }

    /*Excel导入，根据表单导入及验证*/
    public function InExcel($file = '')
    {
        if ($file) {
            $ExcelService = new ExcelService();
            return $ExcelService->Import($file);
        }
        $arr = input("data");
        $RuinuoLogic = new RuinuoLogic();
        $data = [];
        foreach ($arr as $k => $v) {
            $item = $RuinuoLogic->Add($v);
            $v['msg'] = $item['msg'];
            $v['is_fail'] = 2;
            if ($item['code'] != 200) {
                $v['is_fail'] = 1;
            }
            $data[] = $v;
        }
        return ['code' => 200, 'msg' => '', 'data' => $data];
    }

    /*Excel导出下载，根据列表生成*/
    public function OutExcel($params = [])
    {
        $Power=new PowerService();
        $ListsService=new ListsService();
        $current= !empty($params['current']) ? $params['current'] : 1;
        $orderRaw=!empty($params['raw']) ? $params['raw'] : 'a.create_time desc';
        $field=$Power->IndexField('a.id,a.table_name,a.app,a.controller,a.action,a.delete_ip,a.delete_id,a.delete_type,a.is_status,a.create_id,a.create_time','admin:recycle');
        $lists = $this->Mode()
            ->field($field)
            ->where($this->SetWhere($current))
            ->where($this->QueryWhere($params))
            ->orderRaw($orderRaw)
            ->select()
            ->toArray();
        $lists=$ListsService->ListData($lists);//转换数据
        $ExcelService = new ExcelService();
        $excel_field=[
            ['field'=>'table_name','title'=>'表名'],
            ['field'=>'app','title'=>'应用名称'],
            ['field'=>'controller','title'=>'控制器名称'],
            ['field'=>'action','title'=>'方法名称'],
            ['field'=>'delete_ip','title'=>'删除者IP'],
            ['field'=>'delete_id','title'=>'删除ID'],
            ['field'=>'delete_type','title'=>'删除方式'],
            ['field'=>'is_status','title'=>'是否恢复'],
            ['field'=>'create_id','title'=>'操作人'],
            ['field'=>'create_time','title'=>'删除时间'],
        ];
        $data = [[
            'title' => '数据回收',
            'cols' => $ExcelService->Filterate($excel_field,$field),//根据数据输出字段过滤cols
            'list' => $lists
        ]];
        $ExcelService->Out($data, '数据回收');
    }

    /*Excel导入模板下载，根据表单生成*/
    public function TemplateExcel()
    {
        $field=[
            ['field'=>'table_name','title'=>'表名'],
            ['field'=>'app','title'=>'应用名称'],
            ['field'=>'controller','title'=>'控制器名称'],
            ['field'=>'action','title'=>'方法名称'],
            ['field'=>'delete_ip','title'=>'删除者IP'],
            ['field'=>'delete_id','title'=>'删除ID'],
            ['field'=>'delete_type','title'=>'删除方式'],
            ['field'=>'delete_data','title'=>'删除数据'],
        ];
        $ExcelService = new ExcelService();
        $ExcelService->Template('数据回收', $field);
    }

}